package com.ly.aidlpratice

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.ly.aidlpratice.aidl.AIDLActivity
import com.ly.aidlpratice.handwritten.HandwrittenService
import com.ly.aidlpratice.local.LocalActivity
import com.ly.aidlpratice.messenger.MessengerActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //通过Binder与本地服务通信
        findViewById<Button>(R.id.button).setOnClickListener {
            gotoActivity(LocalActivity::class.java)
        }

        //通过Messenger与远程服务通信
        findViewById<Button>(R.id.button1).setOnClickListener {
            gotoActivity(MessengerActivity::class.java)
        }

        //通过AIDL绑定远程服务
        findViewById<Button>(R.id.button2).setOnClickListener {
            gotoActivity(AIDLActivity::class.java)
        }

        findViewById<Button>(R.id.button3).setOnClickListener {
            startService(Intent(this, HandwrittenService::class.java))
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        stopService(Intent(this, HandwrittenService::class.java))
    }

    private fun gotoActivity(clazz: Class<*>) {
        startActivity(Intent(this, clazz))
    }
}